﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class  FrmEditTeamBookRoom :BaseForm
    {

        private Hashtable htBookInfo = new Hashtable();
        public BookInfo bookInfo = null;
        public FrmEditTeamBookRoom()
        {
            InitializeComponent();
            InitDictItems();
            txtBookNo.Text =  Public.GetPreOrderNumber();
            txtPreArrive.Value = DateTime.Now.Date;
            txtPreOff.Value = txtPreArrive.Value.AddDays(1);
        }

        private void InitDictItems()
        {
            this.txtRoomType.Items.Clear();
            List<RoomTypeInfo> typeList = BLLFactory<RoomType>.Instance.GetAll();
            foreach (RoomTypeInfo typeInfo in typeList)
            {
                this.txtRoomType.Items.Add(new CListItem(typeInfo.Name));
            }
            txtRoomType.SelectedIndex = 0;
            lbRoomType.Text = txtRoomType.Text;

            //this.txtSource.Items.Clear();
            //this.txtSource.Items.AddRange(DictItemUtil.GetCustomerSource());
            //if (this.txtSource.Items.Count > 0)
            //{
            //    this.txtSource.SelectedIndex = 0;
            //}
        }

        private void btnSave_Click(object sender, EventArgs e)
        {



            if (this.txtBookPeople.Text.Length == 0)
            {
                MessageUtil.ShowTips("预定人不能为空");
                this.txtBookPeople.Focus();
                return;
            }
            if (this.txtBookPeoplePhone.Text.Length == 0)
            {
                MessageUtil.ShowTips("联系电话不能为空");
                this.txtBookPeoplePhone.Focus();
                return;
            }


            if (this.txtName.Text.Length == 0)
            {
                MessageUtil.ShowTips("团队名称不能为空");
                this.txtName.Focus();
                return;
            }
            if (txtPreArrive.Value >= txtPreOff.Value)
            {


                MessageUtil.ShowTips("预离时间必须大于预抵达时间");
                this.txtPreOff.Focus();
                return;
            
            
            }

            if (htBookInfo.Count < 1)
            {

                MessageUtil.ShowTips("请输入预定房间信息");
                return;
            
            }

            try
            {

                if (bookInfo != null)
                {
                    BLLFactory<Book>.Instance.DeleteByCondition("  BookNo='" + bookInfo.BookNo + "' ");

                }

                foreach (string item in htBookInfo.Keys)
                {
                    BookInfo mbookInfo = new BookInfo();
                    mbookInfo.RoomNo = "未指定";
                    mbookInfo.BookNo = txtBookNo.Text;
                    mbookInfo.RoomType = item;
                    mbookInfo.CustomerCompany = this.txtCompany.Text;
                    mbookInfo.CustomerName = this.txtName.Text;
                    mbookInfo.CustomerTel = this.txtBookPeoplePhone.Text;
                    mbookInfo.Note = this.txtRemark.Text;
                    mbookInfo.PreArrivalTime = this.txtPreArrive.Value;
                    mbookInfo.PreOffTime = this.txtPreOff.Value;
                    mbookInfo.Source = "";
                    mbookInfo.BookPeople = txtBookPeople.Text;
                    mbookInfo.BookPeoplePhone = txtBookPeoplePhone.Text;
                    mbookInfo.IsFixRoomNo = false;
                    mbookInfo.RoomCount = Convert.ToInt32(htBookInfo[item].ToString());
                    mbookInfo.Note = txtRemark.Text;
                    mbookInfo.IsTeam = 1;

                    BLLFactory<Book>.Instance.Insert(mbookInfo);

                }
                MessageUtil.ShowTips("预定客房成功");
                this.DialogResult = DialogResult.OK;

            }
            catch (Exception ex)
            {

                LogHelper.Error(ex);
                MessageUtil.ShowError(ex.Message);
            
            
            }

        }

        /// <summary>
        /// 显示可用房源列表
        /// </summary>
        private void ShowRoomCanUseInfo()
        {
            DataGridViewTextBoxColumn column=null;
            dgView.Columns.Clear();
            DataTable roomdt = new DataTable();
            DateTime arriveDate = txtPreArrive.Value;
            DateTime OffDate = txtPreOff.Value;
            int dayCount = (OffDate - arriveDate).Days + 1;
            for (int i = 0; i < dayCount; i++)
            {
                column = new DataGridViewTextBoxColumn();
                column.HeaderText = arriveDate.AddDays(i).ToString("yyyy.MM.dd");
                column.DisplayIndex = i;
                column.Width = 80;
                column.DataPropertyName = arriveDate.AddDays(i).ToString("yyyy.MM.dd");
                column.Name = arriveDate.AddDays(i).ToString("yyyy.MM.dd");
                column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dgView.Columns.Add(column);
                roomdt.Columns.Add(arriveDate.AddDays(i).ToString("yyyy.MM.dd"), typeof(int));
            
            
            }

            //生成数据
            List<RoomInfo> roomlist = BLLFactory<WHC.Hotel.BLL.Room>.Instance.FindAllRooms("  RoomType='"+txtRoomType.Text+"' ");
            List<BookInfo> bookList = BLLFactory<WHC.Hotel.BLL.Book>.Instance.FindBookByCondition("  RoomType='" + txtRoomType.Text + "' ");
            DataRow dr = roomdt.NewRow();
            //初始化
            for (int j = 0; j < dayCount; j++)
            {

                dr[arriveDate.AddDays(j).ToString("yyyy.MM.dd")] = 0;

            }
            int sum = roomlist.Count;//总房数;
            int canUse = 0;
            int bookNum = 0;
            for (int k = 0; k < dayCount; k++)
            {
                
                //1:判断当天可能的房间数
                canUse = 0;
                foreach (RoomInfo entity in roomlist)
                {



                    switch (entity.Status)
                    {
                        case Entity.RoomStatus.空净房:
                        case Entity.RoomStatus.空脏房:
                            canUse++;
                            break;
                        case Entity.RoomStatus.住净房:
                        case Entity.RoomStatus.住脏房:
                            if (!(arriveDate.AddDays(k) >= entity.OrderInfo.CreateDate && arriveDate.AddDays(k) < entity.OrderInfo.PreOffDate))
                            {

                                canUse++;

                            }
                            break;
                        case Entity.RoomStatus.预定房:
                             canUse++;
                             break;


                    }



                }//判断当天可能的房间数end


                //2:扣除当天预订数
                if (bookList.Count > 0)
                {
                    foreach (BookInfo item in bookList)
                    {

                        if (arriveDate.AddDays(k) >= item.PreArrivalTime && arriveDate.AddDays(k) < item.PreOffTime)
                        {


                            bookNum = bookNum + item.RoomCount;//当天需要天数


                        }

                    }

                }
               
                //3.统计
                dr[arriveDate.AddDays(k).ToString("yyyy.MM.dd")] = canUse - bookNum;


            }


      

            roomdt.Rows.Add(dr);
            this.dgView.DataSource = roomdt;


         
        
        }

        private void txtRoomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowRoomCanUseInfo();
            lbRoomType.Text = txtRoomType.Text;

        }

        private void txtPreArrive_ValueChanged(object sender, EventArgs e)
        {
            ShowRoomCanUseInfo();
        }

        private void txtPreOff_ValueChanged(object sender, EventArgs e)
        {
            ShowRoomCanUseInfo();
        }

        private void FrmBookRoomNoFixNum_Load(object sender, EventArgs e)
        {
            ShowRoomCanUseInfo();
            if (bookInfo != null)
            {
                //RoomInfo info = BLLFactory<Room>.Instance.FindByRoomNo(BookInfo.RoomNo);
                this.txtRoomType.Text = bookInfo.RoomType;
                this.txtBookNo.Text = bookInfo.BookNo;
                this.txtCompany.Text = bookInfo.CustomerCompany;
                this.txtName.Text = bookInfo.CustomerName;
                this.txtRemark.Text = bookInfo.Note;
                this.txtPreArrive.Value = bookInfo.PreArrivalTime;
                this.txtPreOff.Value = bookInfo.PreOffTime;
                // this.txtRetention.Value = BookInfo.RetentionTime;
               // this.txtSource.Text = BookInfo.Source;
                this.txtBookPeople.Text = bookInfo.BookPeople;
                this.txtBookPeoplePhone.Text = bookInfo.BookPeoplePhone;
                List<BookInfo> list = BLLFactory<Book>.Instance.FindTeamBookList(bookInfo.BookNo);
                foreach (BookInfo item in list)
                {


                    htBookInfo.Add(item.RoomType, item.RoomCount.ToString());

                
                
                }
                RefreshListView();

               
            }
            else
            {

                this.txtBookNo.Text = Public.GetPreOrderNumber();
            
            
            }


        }

        private void btnAddRoomType_Click(object sender, EventArgs e)
        {

             AddedBookRoomType();

        }

        /// <summary>
        /// 删除选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvwBookRooms_MouseDoubleClick(object sender, MouseEventArgs e)
        {


            if (this.lvwBookRooms.SelectedItems.Count == 0)
            {
                return;
            }

            ListViewItem listViewItem = this.lvwBookRooms.SelectedItems[0];
            if (listViewItem != null)
            {
                this.htBookInfo.Remove(listViewItem.SubItems[1].Text);
            }
            RefreshListView();
            
        }

        private void RefreshListView()
        {


            int serialNum = 1;
            this.lvwBookRooms.Items.Clear();
            foreach (string item in this.htBookInfo.Keys)
            {

                ListViewItem listitem = new ListViewItem(serialNum.ToString());
                listitem.SubItems.Add(item);
                listitem.SubItems.Add(htBookInfo[item].ToString());
                this.lvwBookRooms.Items.Add(listitem);

                serialNum++;
            }
        
        
        }

        private void AddedBookRoomType()
        {
            this.lvwBookRooms.Items.Clear();
            if (htBookInfo.Contains(this.txtRoomType.Text))
            {
                this.htBookInfo[txtRoomType.Text] = txtRoomCount.Text;
            }
            else
            {
                htBookInfo.Add(txtRoomType.Text, txtRoomCount.Text);
            
            }
            RefreshListView();
        }

        private void btnCanel_Click(object sender, EventArgs e)
        {


            this.Close();
        }

       
    }
}
